//
//  YiHomeListCtrl.swift
//  yiplayer
//
//  Created by wkun on 2024/6/25.
//

import UIKit

private let reuseIdentifier = "Cell"

class YiHomeListCtrl: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    
    var datas: [YiHomeListCellModel]?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        
        // Register cell classes
        self.collectionView!.register(YiHomeListCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        let layout = UICollectionViewFlowLayout()
        self.collectionView.collectionViewLayout = layout
        
        self.view.backgroundColor = .clear
        self.collectionView.backgroundColor = .clear
        // Do any additional setup after loading the view.
        
        self.collectionView.alwaysBounceVertical = true
        
        self.loadDatas()
    }
    
    func loadDatas() {
        let names = ["照片", "文件", "SMB", "FTP", "阿里盘", "百度盘", "流媒体", "传文件", "历史",  "设置"]
        let imgs = ["photo", "doc", "smb", "ftp", "alipan","baidupan","url", "wifi", "history", "set"]
        var arr = [YiHomeListCellModel]()
        for i in 0..<names.count {
            let m = YiHomeListCellModel()
            m.name = names[i]
            m.imgName = imgs[i]
            arr.append(m)
        }
        self.datas = arr
        self.collectionView.reloadData()
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return self.datas?.count ?? 0
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
    
        // Configure the cell
        if let lc = cell as? YiHomeListCell {
            if let m = self.model(datas, indexPath) as? YiHomeListCellModel {
                lc.imgView.setImg(m.imgName)
            }
        }
    
        return cell
    }

    let toedge = 40.0
    // MARK: UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let fr = collectionView.frame.size
        let celledge = 2.0
        let gap = toedge - 2.0*celledge
        var column = 4.0
        if fr.width < fr.height {
            column = 2.0
        }
        let edge = toedge - celledge
        let scale = 176.0/106.0
        
        let w1 = (fr.width-2.0*edge-(column-1.0)*gap)/column
        
        
        return .init(width: w1, height: w1/scale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        let celledge = 2.0
        let gap = toedge - 2.0*celledge
        return gap
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        let celledge = 2.0
        let gap = toedge - 2.0*celledge
        return gap
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let e8: CGFloat = toedge-2.0
        return .init(top: e8, left: e8, bottom: e8, right: e8)
    }
    
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        collectionView.collectionViewLayout.invalidateLayout()
    }
}

class YiHomeListCell: UICollectionViewCell{
    
    let imgView = UIImageView()
    let textL = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let w: CGFloat = 176
        let scale: CGFloat = 176.0/106.0
        let h: CGFloat = w/scale
        
        let edge = 2.0
        
        imgView.pin(.wh(w, h)).radius(15.0).shadow(0.1)
        imgView.addTo(self.contentView).makeCons { make in
            make.left.top.equal(edge)
            make.bottom.right.equal(-edge)
        }
        imgView.mode(.scaleAspectFill)
        
        textL.font(16).color(Color6).embedIn(imgView).align(.center)
        
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class YiHomeListCellModel: NSObject {
    var name: String?
    var imgName: String?
}
